package com.etc.dao.impl;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.etc.dao.OrderInfoDao;
import com.etc.entity.Games;
import com.etc.entity.OrderInfo;
import com.etc.util.DBUtil;

public class OrderInfoDaoImpl implements OrderInfoDao{
	/**
	 * 添加一条未支付状态的订单信息
	 */
	@Override
	public int add(OrderInfo orderInfo) {
		return DBUtil.update("insert into order_info(order_no,total,customer_id,status) values (?,?,?,0)", orderInfo.getOrderNo(),orderInfo.getTotal(),orderInfo.getCustomerId());
	}
	/**
	 * 更改订单支付状态支付状态
	 */
	@Override
	public int updateStatus(Integer status, String payTime, String orderNo) {
		StringBuilder sql = new StringBuilder("update order_info set status = ");
		sql.append(status);
		if(payTime!=null&&!"".equals(payTime)) {
			sql.append(" , pay_time = '").append(payTime).append("'");
		}
		sql.append(" where order_no = '").append(orderNo).append("'");
		
		return DBUtil.update(sql.toString());
	}
	/**
	 * 通过订单号查询订单信息
	 */
	@Override
	public OrderInfo findByOrderNo(String orderNo) {
		ResultSet rs = DBUtil.query("select total,customer_id,status,create_time,pay_time from order_info where order_no = ?", orderNo);
		OrderInfo orderInfo = null;
		try {
			while (rs.next()) {
				BigDecimal total = rs.getBigDecimal("total"); 
				Integer customerId = rs.getInt("customer_id");   
				Integer status = rs.getInt("status");   
				String createTime = rs.getString("create_time");     
				String payTime = rs.getString("pay_time");   
				
				// 将每一条记录封装成一个Java对象
				orderInfo = new OrderInfo();
				orderInfo.setOrderNo(orderNo);
				orderInfo.setTotal(total);
				orderInfo.setCustomerId(customerId);
				orderInfo.setStatus(status);
				orderInfo.setCreateTime(createTime);
				orderInfo.setPayTime(payTime);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return orderInfo;
	}
}
